package com.zpark.signup.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zpark.signup.dto.UserDto;
import com.zpark.signup.dto.UserPwdDto;
import com.zpark.signup.entity.User;
import com.zpark.signup.service.UserService;
import com.zpark.signup.mapper.UserMapper;
import com.zpark.signup.utils.R;
import com.zpark.signup.utils.RespPageBean;
import jdk.internal.org.objectweb.asm.TypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author YYS
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2023-09-25 11:46:09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserMapper userMapper;


    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @Override
    public R registerUser(User user) {
//        1.查询用户名是否存在
        R r = selectUserByUsername(user.getUsername());
        if (r.getCode() != 200) {
            return r;
        }
        R r1 = selectUserByEmail(user.getEmail());
//        2.验证邮箱是否已被使用
        if (r1.getCode() != 200) {
            return r1;
        }
//        执行新增用户
        int i = userMapper.insert(user);
        if (i > 0) {
            R.ok("注册成功！");
        }
        return R.error("注册失败");
    }

    @Override
    public R deleteUser(Integer id) {
        User user = new User();
        user.setStatus(0);
        user.setId(1);
        int i = userMapper.updateById(user);
        if (i > 0) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    /**
     * 修改用户接口信息
     *
     * @param user
     * @return
     */
    @Override
    public R updateUser(User user) {
        user.setPassword(null);
        boolean b = this.updateById(user);
        if (b) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    @Override
    public RespPageBean selectUser(UserDto userDto) {
        Page<User> page = this.page(new Page<User>().setSize(userDto.getSize()).setCurrent(userDto.getCurrentPage()), new LambdaQueryWrapper<User>()
                .eq(userDto.getClazzId() != null, User::getClazzId, userDto.getClazzId())
                .like(userDto.getUsername() != null, User::getUsername, userDto.getUsername())
                .like(userDto.getIdentity() != null, User::getIdentity, userDto.getIdentity()));

        return new RespPageBean(page.getRecords(), page.getTotal(), page.getSize(), page.getCurrent());
    }

    @Override
    public R selectUserByUsername(String username) {
        //        1.查询用户名或邮箱是否已重复
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", 1);

        User userDb1 = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username)
        );
        if (userDb1 != null) {
            return R.error("用户名已存在，请重新输入");
        }
        return R.ok("用户名可以使用！");
    }

    @Override
    public R selectUserByEmail(String email) {
        if (userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getEmail, email)) != null) {
            return R.error("邮箱已存在，请重新输入");
        }
        return R.ok("邮箱可以使用！");
    }

    /**
     * 修改用户密码
     * 密码使用BCript加密
     *
     * @return
     */
    @Override
    public R updateUserPassword(UserPwdDto userPwdDto) {
//        判断原密码与数据库是否相同
        if (BCrypt.checkpw(userPwdDto.getOldPassword(), selectPasswordByUserId(userPwdDto.getUserId()))) {
//            对新密码进行加密
            String hashpw = BCrypt.hashpw(userPwdDto.getNewPassword(), BCrypt.gensalt());
            User user = new User();
            user.setPassword(hashpw);
            user.setId(userPwdDto.getUserId());
//            修改密码
            return userMapper.updateById(user) > 0 ? R.ok("修改密码成功") : R.error("密码修改失败");
        }
        return R.error("原密码不正确");
    }

    /**
     * 根据用户id获取用户密码
     *
     * @param userId
     * @return
     */
    @Override
    public String selectPasswordByUserId(Integer userId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getId, userId)
                .select(User::getPassword);
        User user = userMapper.selectOne(queryWrapper);

        return user.getPassword();
    }

    /**
     * 根据id查询单个用户信息
     *
     * @param id
     * @return
     */
    @Override
    public R selectUser(Integer id) {
        return userMapper.selectById(id) != null ? R.ok("查询成功", userMapper.selectById(id)) : R.error("查询失败");
    }
}




